﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ComputerVision.Data;
using ComputerVision.Data.Repositories;

namespace ComputerVision.Services
{
    public class TuyenTruyenHuanLuyenService : IService<TuyenTruyenHuanLuyen>
    {
        private readonly IEntityRepository<TuyenTruyenHuanLuyen> _tuyenTruyenHuanLuyenRepository;
        public TuyenTruyenHuanLuyenService(IEntityRepository<TuyenTruyenHuanLuyen> tuyenTruyenHuanLuyenRepository)
        {
            _tuyenTruyenHuanLuyenRepository = tuyenTruyenHuanLuyenRepository;
        }

        public TuyenTruyenHuanLuyen Create(TuyenTruyenHuanLuyen t)
        {
            try
            {
                if (t == null)
                    throw new ArgumentNullException("entity");
                _tuyenTruyenHuanLuyenRepository.InsertOnCommit(t);
                _tuyenTruyenHuanLuyenRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                    foreach (var validationError in validationErrors.ValidationErrors)
                        msg += string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage) + Environment.NewLine;

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
            return t;
        }

        public TuyenTruyenHuanLuyen Update(TuyenTruyenHuanLuyen t)
        {
            TuyenTruyenHuanLuyen item;
            try
            {
                item = _tuyenTruyenHuanLuyenRepository.Get(t.Id);
                item.Ngay = t.Ngay;
                item.ThoiLuong = t.ThoiLuong;
                item.NoiDung = t.NoiDung;
                item.LucLuongPCCCCoSo = t.LucLuongPCCCCoSo;
                item.CanBoQuanLy = t.CanBoQuanLy;
                item.NguoiLaoDong = t.NguoiLaoDong;
                item.DoiTuongKhac = t.DoiTuongKhac;
                item.SoGiayCN = t.SoGiayCN;
                //item.PhieuDieuTraCoSoId = t.PhieuDieuTraCoSoId;
                //item.Created = DateTime.Now;
                //item.CreatedBy = t.CreatedBy;
                _tuyenTruyenHuanLuyenRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                    foreach (var validationError in validationErrors.ValidationErrors)
                        msg += Environment.NewLine +
                               string.Format("Property: {0} Error: {1}", validationError.PropertyName,
                                   validationError.ErrorMessage);

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
            return item;
        }

        public TuyenTruyenHuanLuyen Detail(Guid id)
        {
            var item = _tuyenTruyenHuanLuyenRepository.Get(id);
            return item;
        }

        public List<TuyenTruyenHuanLuyen> ToList()
        {
            var item = _tuyenTruyenHuanLuyenRepository.GetAll().ToList();
            return item;
        }

        public void Delete(TuyenTruyenHuanLuyen t)
        {
            try
            {
                if (t == null)
                    throw new ArgumentNullException("entity");
                _tuyenTruyenHuanLuyenRepository.DeleteOnCommit(t);
                _tuyenTruyenHuanLuyenRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;

                foreach (var validationErrors in dbEx.EntityValidationErrors)
                    foreach (var validationError in validationErrors.ValidationErrors)
                        msg += Environment.NewLine + string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
        }
    }
}
